package com.haiqi.modules.ship.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.CommunicationInfo;
import com.haiqi.common.persistence.entity.ShipInfo;
import com.haiqi.modules.ship.service.ShipCommunicationInfoService;
import com.haiqi.modules.ship.service.ShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hythzx on 2016/5/26.
 */
@Controller
@RequestMapping("/manage/ship/communication")
@SafeAuthority(menuCode = "fleetManage,monitorLocation")
public class ShipCommunicationController extends BaseController{

    private String menuCode = "fleetManage";

    @Autowired
    private ShipService shipService;

    @Autowired
    private ShipCommunicationInfoService service;

    @RequestMapping("/list")
    public String list(CommunicationInfo communicationInfo, PageBean pageBean, HttpSession session, Model model){
        generateMenuCode(session, menuCode);
        PageInfo<CommunicationInfo> pageInfo = service.getDataList(communicationInfo, pageBean);
        ShipInfo shipInfo = shipService.getShipInfo(communicationInfo.getShipid());
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("param",communicationInfo);
        model.addAttribute("shipInfo",shipInfo);
        return "/modules/ship/communication/list";
    }

    @RequestMapping("/ajax/list")
    @ResponseBody
    public List<CommunicationInfo> getJsonList(CommunicationInfo communicationInfo, HttpSession session){
        List<CommunicationInfo> shipInfoList = service.getDataList(communicationInfo);
        return shipInfoList;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public CommunicationInfo getShipInfo(CommunicationInfo communicationInfo){
        communicationInfo = service.getCommunicationInfo(communicationInfo.getId());
        CommunicationInfo info = new CommunicationInfo();
        info.setShipid(communicationInfo.getShipid());
        info.setCommunicateType(communicationInfo.getCommunicateType());
        info.setCommunicatePerson(communicationInfo.getCommunicatePerson());
        info.setCommunicateNumber(communicationInfo.getCommunicateNumber());
        info.setId(communicationInfo.getId());
        return info;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map updateShipInfo(CommunicationInfo communicationInfo, HttpSession session){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = service.update(communicationInfo);
        response.put("success",success);
        return response;
    }
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(CommunicationInfo communicationInfo){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = service.delete(communicationInfo.getId());
        response.put("success",success);
        return response;
    }

}
